<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Spots extends CI_Controller {

    function __construct() {
        date_default_timezone_set('UTC');
        parent::__construct();
        $this->load->helper('form');
        $this->load->helper('file');
        $this->load->helper('url');
        $this->load->library('qqUploadedFileXhr');
        $this->load->library('s3');
        $this->load->model('settings');
        $this->load->model('manage_login');
        $this->load->model('share_media');
        $this->load->model('manage_spots');
    }
    public function index() {
        $data = $this->manage_login->manageLogin();
        $data['spot'] = $this->manage_spots->getSpotID("5158d3b74e546c0f6c000033");
        $this->lang->load('main', 'english');
        $this->load->view('spots/spots',$data);
    }
    function terms(){
        $data = $this->manage_login->manageLogin();
        $this->lang->load('main', 'english');
        $this->load->view('spots/terms',$data);
    }
    function addSpot(){
        $data = array(
          'name' => $this->input->post('name'),
          'difficulty' => $this->input->post('diff'),
          'creativity' => $this->input->post('creativity'),
          'address' => $this->input->post('address'),
          'access' => $this->input->post('access'),
          'description' => $this->input->post('infos'),
          'user_id' => $this->input->cookie('LEAPSESSID'),
          'continent'=>$this->input->post('continent'),
          'country'=>$this->input->post('country'),
          'region'=>$this->input->post('region'),
        );
        $spotID = $this->manage_spots->addSpot($data);
        if($spotID){
            echo $spotID;
        }
        else{
            echo false;
        }
    }
     function upload_pics(){
        // list of valid extensions, ex. array("jpeg", "xml", "bmp")
        $allowedExtensions = array("gif","jpg","png","JPG","jpeg");
// max file size in bytes

        $uploader = new qqFileUploader($allowedExtensions);

// Call handleUpload() with the name of the folder, relative to PHP's getcwd()
        $result = $uploader->handleUpload('./uploads/');
        if(!$result){
            echo "Something Went Wrong!";
        }
        $org_picture = $this->s3->putObjectFile('./uploads/'.$uploader->getName(), "leap_s3", "spots/".$this->input->get('id')."/".$uploader->getName(), "public-read");
        $this->manage_spots->updatePictureSpot($this->input->get('id'),"images","https://s3.amazonaws.com/leap_s3/spots/".$this->input->get('id')."/".$uploader->getName());

        $this->thumbImage($uploader->getName());
        $thmub_picture = $this->s3->putObjectFile('./uploads/' . str_replace(".", "_thumb.", $uploader->getName()), "leap_s3", "spots/".$this->input->get('id')."/". str_replace(".", "_thumb.", $uploader->getName()), "public-read");
        $pic_thumb = "https://s3.amazonaws.com/leap_s3/spots/".$this->input->get('id')."/". str_replace(".", "_thumb.", $uploader->getName());
        $this->manage_spots->updatePictureSpot($this->input->get('id'),"thumb_images",$pic_thumb);
        $this->settings->SureRemoveDir('./uploads', false);
        if($org_picture && $thmub_picture){
            $data['success'] = '{"success":true}';
            echo json_encode($data);
        }
        else{
            echo "Something Went Wrong!";
        }

    }
    function thumbImage($fileName){
        $config['image_library'] = 'gd2';
        $config['source_image'] = $_SERVER['DOCUMENT_ROOT'] . '/uploads/' . $fileName;
        $config['new_image'] = $_SERVER['DOCUMENT_ROOT'] . '/uploads/' . $fileName;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['thumb_marker'] = "_thumb";
        $config['width'] = 200;
        $config['height'] = 0; // No restraint on height


        $this->load->library('image_lib', $config);
        if (!$this->image_lib->resize()){
            return $this->image_lib->display_errors();
        }
        $this->image_lib->clear();
    }
    function getSpotsByRegion(){
        
        $data['spots'] = $this->manage_spots->getSpotsByRegions($this->input->get('region'));
        echo json_encode($data);
    }
    function getSpotInfos(){
        
        $data = $this->manage_spots->getSpotInfos($this->input->get('spotID'));
        echo json_encode($data);
    }

}
